package org.six11.olive.collabclient.server;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.six11.util.Debug;
import org.six11.util.adt.MultiState;

/**
 * A very simple listener that reports when sessions are created and destroyed.
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class WebappEventListener implements HttpSessionListener {

  public void sessionCreated(HttpSessionEvent ev) {
    ev.getSession().setMaxInactiveInterval(60 /* seconds */ * 40 /* minutes */);
    bug("Session created: " + ev.getSession().getId());
  }

  public void sessionDestroyed(HttpSessionEvent ev) {
    bug("Session destroyed: " + ev.getSession().getId());
    MultiState state = (MultiState) ev.getSession().getServletContext().getAttribute("state");
    StateTool tool = new StateTool(state);
    tool.removeSessionFromRoom(ev.getSession().getId());
  }

  private static void bug(String what) {
    Debug.out("WebappEventListener", what);
  }

}
